package com.webwork.mood.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webwork.mood.system.dao.BranchDao;
import com.webwork.mood.system.dataobject.Branch;
import com.webwork.mood.system.service.BranchService;
import com.webwork.mood.util.Paging;
import com.webwork.mood.util.StrManager;

@Service
public class BranchServiceImpl implements BranchService {

	@Autowired
	private BranchDao branchDao;
	
	@Override
	public void insert(Branch branch) {
		branchDao.insert(branch);
	}
	
	@Override
	public void update(Branch branch) {
		branchDao.update(branch);
	}
	
	@Override
	public void save(Branch branch) {
		if (this.get(branch.getBranchId()) != null) {
			this.update(branch);
		} else {
			this.insert(branch);
		}
	}
	
	@Override
	public void delete(String branchId) {
		branchDao.delete(branchId);
	}
	
	@Override
	public List<Branch> query(Branch branch) {
		return this.query(branch, new Paging(0, 0));
	}
	
	@Override
	public List<Branch> query(Branch branch, Paging page) {
		return branchDao.query(branch, page.getCurrPage(), page.getPageSize());
	}
	
	@Override
	public Branch get(String branchId) {
		if (StrManager.isEmpty(branchId)) {
			return null;
		}
		Branch queryObj = new Branch();
		queryObj.setBranchId(branchId);
		List<Branch> branch = this.query(queryObj);
		if (branch != null && branch.size() > 0) {
			if (branch.size() > 1) {
				System.out.println("存在不止一条记录!");
			} else {
				return branch.get(0);
			}
		} else {
			System.out.println("找不到该账号!");
		}
		return null;
	}

	@Override
	public int count(Branch branch) {
		return branchDao.count(branch);
	}

}
